using BlazorAdmin.Models;
using BlazorAdmin.Interface;
using BlazorAdmin.Web.Shared.Services;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;

namespace BlazorAdmin.Web.Shared.Components
{
    public partial class OrganizeEdit
    {
        [Parameter]
        public Organize? Value { get; set; }

        [Parameter]
        public EventCallback<Organize>? ValueChanged { get; set; }

        private List<TreeViewItem<Organize>>? OrganizeTree { get; set; }

        private List<SelectedItem> Options = new List<SelectedItem>()
        {
            new SelectedItem("1", "启用"),
            new SelectedItem("0", "禁用"),
        };

        protected override async void OnInitialized()
        {
            base.OnInitialized();
            
            if (Value?.ID == 0)
                Value.OrgIcon = "fas fa-layer-group";

            OrganizeTree = await OrganizeService?.GetOrganizeTree(AppContext?.LoginName, true);

            if (!string.IsNullOrEmpty(Value?.ParentOrgCode)) // 存在父节点，则遍历Tree获取父节点的name
                OrganizeTree.ForEach(async t => RecurseSetSelectedOrganizeName(t));
        }

        private void RecurseSetSelectedOrganizeName(TreeViewItem<Organize> parentItem)
        {
            foreach (var item in parentItem.Items)
            {
                if (item.Value.OrgCode == Value?.ParentOrgCode)
                    SelectedOrganizeName = item.Value.OrgName;

                if(item.Value.OrgCode == Value.OrgCode) // 禁用自己
                    item.IsDisabled = true;

                if (item.Items.Count > 0)
                    RecurseSetSelectedOrganizeName(item);
            }
        }

        private DialogOption? Option { get; set; }

        private string? SelectedOrganizeName { get; set; }

        private async Task OnSelectMenu()
        {
            Option = new DialogOption()
            {
                Title = "选择父组织",
                IsScrolling = true,
                Size = Size.Medium,
                BodyTemplate = BootstrapDynamicComponent.CreateComponent<TreeView<Organize>>(new Dictionary<string, object?>
                {
                    [nameof(TreeView<Organize>.Items)] = OrganizeTree,
                    [nameof(TreeView<Organize>.OnTreeItemClick)] = async (TreeViewItem<Organize> item) => {
                        if (Value.OrgCode != item.Value.OrgCode) // 不能选择自己
                        {
                            Value.ParentOrgCode = item.Value.OrgCode;
                            SelectedOrganizeName = item.Value.OrgName;
                        }
                        else
                        {
                            Value.ParentOrgCode = null;
                            SelectedOrganizeName = string.Empty;

                            MsgService?.Show(new() { Content = "不允许设置自身节点为父节点" });
                        }
                    }
                }).Render()
            };

            await DialogService.Show(Option);
        }

        private async Task OnClearText()
        {
            Value.ParentOrgCode = null;
            SelectedOrganizeName = string.Empty;
        }

        private Task OnToggleIconDialog() => DialogService.Show(new DialogOption()
        {
            Title = "选择图标",
            ShowFooter = false,
            Component = BootstrapDynamicComponent.CreateComponent<MenuIconList>(new Dictionary<string, object?>()
            {
                [nameof(MenuIconList.Value)] = Value?.OrgIcon,
                [nameof(MenuIconList.ValueChanged)] = EventCallback.Factory.Create<string?>(this, v => Value.OrgIcon = v)
            })
        });

        private Task OnClearIcon()
        {
            Value.OrgIcon = null;

            return Task.CompletedTask;
        }

        #region Inject 注入的服务

        [Inject]
        private IOrganize? OrganizeService { get; set; }

        [Inject]
        private MessageService? MsgService { get; set; }

        [Inject]
        private DialogService? DialogService { get; set; }

        [Inject]
        private BootstrapAppContext? AppContext { get; set; }

        #endregion
    }
}
